﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Experimentor
{
    public class DatasaveObject
    {
        private Dictionary<string, Dictionary<string, object>> _objects;
        private bool _preview;
        private string _session;
        private string _dir;
        private ExcelPackage _package;
        
        public DatasaveObject(bool preview, string session, string dir, ExcelPackage package)
        {
            this._objects = new Dictionary<string, Dictionary<string, object>>();
            this._preview = preview;
            this._session = session;
            this._dir = dir;
            this._package = package;
        }

        public bool Preview
        {
            get { return this._preview; }
            private set { }
        }
        
        public string Session
        {
            get { return this._session; }
            private set { }
        }
        
        public string Directory
        {
            get { return this._dir; }
            private set { }
        }

        public ExcelPackage Package
        {
            get { return this._package; }
            private set { }
        }

        public void addSaveObject(string table, string column, object tostore)
        {
            lock (this)
            {
                if (!this._objects.ContainsKey(table)) this._objects.Add(table, new Dictionary<string, object>());
                this._objects[table][column] = tostore;
            }
        }

        public object getSavedObject(string table, string column)
        {
            lock (this)
            {
                if (!this._objects.ContainsKey(table)) return null;
                if (!this._objects[table].ContainsKey(column)) return null;
                return this._objects[table][column];
            }
        }

        public void addOtherDatasaveObject(DatasaveObject other)
        {
            lock (this)
            {
                foreach (KeyValuePair<string, Dictionary<string, object>> outer in other._objects)
                {
                    foreach (KeyValuePair<string,object> inner in outer.Value)
                    {
                        if (!this._objects.ContainsKey(outer.Key)) this._objects.Add(outer.Key, new Dictionary<string, object>());
                        this._objects[outer.Key][inner.Key] = inner.Value;
                    }
                }

            }
        }

        public void writeToExcel()
        {
            foreach (KeyValuePair<string, Dictionary<string, object>> outer in this._objects)
            {
                foreach (KeyValuePair<string, object> inner in outer.Value)
                {
                    ExcelSave.storeValue(this._package.Workbook, outer.Key, this._session, inner.Key, inner.Value);
                }
            }
        }
    }
}
